<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseController;
use App\Services\Tools\CommonService;
use App\Services\Tools\ExportService;
use Illuminate\Http\Request;    //引用http请求类
use App\Exceptions\BasicException;  //引入框架异常处理类
use Illuminate\Http\Resources\Json\PaginatedResourceResponse;
use Illuminate\Support\Facades\Hash;    //引入hash
use App\Services\Cache\AdminCacheService;   //admin缓存
use App\Services\Tools\JwtService;    //引入JWT服务
use App\Services\Tools\FileService;   //引入文件处理类
use App\Response\Response;
use App\Models\MainDB\GiveGiftDay;
use Monolog\Handler\IFTTTHandler;

class FishController extends BaseController {

    public $type, $pot_name;

    public function __construct(Request $request) {
        $this->type = $request->input('type', 3);
        switch ($this->type) {
            case 1:
                $this->pot_name = '1连奖池';
                break;
            case 2:
                $this->pot_name = '10连奖池';
                break;
            case 3:
                $this->pot_name = '100连奖池';
                break;
            case 4:
                $this->pot_name = '独立奖池';
                break;
            case 5:
                $this->pot_name = '爆率奖池';
                break;
            default:
                $this->pot_name = '100连奖池';
        }
    }


    /**
     * @api {post} /fish/set_alone_user 设置钓鱼独立奖池用户
     * @apiVersion 0.1.0
     * @apiName 设置钓鱼独立奖池用户
     * @apiGroup 游戏管理
     *
     * @apiParam {string} user_code  用户编号，非必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     *
     */
    public function setAloneUser(Request $request) {
        $user_code = $request->input('user_code', '');
        if (!$user_code || $user_code <= 0) {
            app('repo_config')->delConfig('POOL_USER');
            return Response::sendData(true);
        }
        $where[] = ['user_code', $user_code];
        $user_id = app('repo_user_data')->getUserId($where);
        $c = app('repo_config')->queryConfig('POOL_USER');
        if (!$c) {
            $param = [
                'key' => 'POOL_USER',
                'value' => $user_id,
                'type' => 0,
                'group' => 1,
                'name' => '独立奖池用户',
                'desc' => '独立奖池用户user_id',
                'status' => 0,
                'sort' => 0,
            ];
            app('repo_config')->addConfig($param);
        } else {
            $where_save[] = ['key', 'POOL_USER'];
            $param = [
                'value' => $user_id,
            ];
            app('repo_config')->saveConfig($where_save, $param);
        }
        return Response::sendData(true);
    }


    /**
     * @api {post} /fish/ratio_jackpot  钓鱼开关爆率奖池
     * @apiVersion 0.1.0
     * @apiName 钓鱼开关爆率奖池
     * @apiGroup 游戏管理
     * @apiParam {number} type  类型：3 关闭爆率奖池 5开启爆率奖池，必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     *
     */
    public function ratioJackpot(Request $request) {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'type' => 'required|in:3,5',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $c = app('repo_config')->queryConfig('POOL_TYPE');
        if (!$c) {
            $param = [
                'key' => 'POOL_TYPE',
                'value' => $this->type,
                'type' => 0,
                'group' => 1,
                'name' => '设置奖池类型',
                'desc' => '设置奖池类型3百连;5爆率',
                'status' => 0,
                'sort' => 0,
            ];
            app('repo_config')->addConfig($param);
        } else {
            $where_save[] = ['key', 'POOL_TYPE'];
            $param = [
                'value' => $this->type,
            ];
            app('repo_config')->saveConfig($where_save, $param);
        }
        return Response::sendData(true);
    }




    /**
     * @api {post} /fish/fishing_gift_list  钓鱼新增、编辑奖品列表
     * @apiVersion 0.1.0
     * @apiName 钓鱼新增、编辑奖品列表
     * @apiGroup 游戏管理
     * @apiSuccessExample 成功响应
          {
            "code": 200,
            "data": [
                {
                    "id(主键id)": 78,
                    "name（礼物名字）": "摘星揽月",
                    "price（价格）": 334400,
                    "picture（图片）": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/user-dir/MtSk6yyRG7.png"
                }
            ]
        }
     *
     */
    public function  fishingGiftList(){
        $fishingGiftList = app('repo_gift')->gameGiftList(2);
        return Response::sendData($fishingGiftList);
    }

    /**
     * @api {post} /fish/this_list  钓鱼本期奖池列表
     * @apiVersion 0.1.0
     * @apiName 钓鱼本期奖池列表
     * @apiGroup 游戏管理
     * @apiParam {number} type    1一连2十连3百连4独立奖池5爆率
     * @apiSuccessExample 成功响应
         {
             "code": 200,
             "data": {
              "alone_user_pool_name(独立奖池用户昵称)": "加勒比海盗",
              "alone_user_pool_code（独立奖池用户编号）": 1,
             "radio_pool_type(爆率奖池：3已关闭  5已开启)": "3",
             "pot_name(奖池名字)": "10连奖池",
             "all_number（剩余礼物数量）": 1000016312,
             "all_money（剩余礼物总金币数量）": 427198848614562,
                 "list": [
                     {
                    "id（主键ID）": 3735,
                      "number（数量）": 9190,
                     "gift_id（奖品ID）": 156,
                     "ratio（爆率）": 0,
                     "name（奖品名字）": "音符",
                     "picture（图片）": "http://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/admin_images/5e61e230d6521.png",
                     "price（价格）": 10
                     }
                  ]
             }
         }
     *
     */
    public function thisPrizeList(Request $request) {

        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'type' => 'filled|numeric|in:1,2,3,4,5',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $type = $request->input('type', 3);
        $giftList = app('repo_win_jackpot')->thisGiftList($type);
        //剩余礼物数量
        $all_number = 0;
        //剩余礼物总金币数量
        $all_money = 0;
        if ($giftList) {
            foreach ($giftList as $key => &$value) {
                $all_number += $value['number'];
                $value['name'] = $value['gift']['name'];
                $value['picture'] = $value['gift']['picture'];
                $value['price'] = $value['gift']['price'];
                $all_money += ($value['number'] * $value['gift']['price']);
                unset($value['gift']);
            }
        }
        $c = app('repo_config')->queryConfig('POOL_TYPE');

        if ($c){
            $radio_pool_type = $c->value;
        }else{
            $radio_pool_type =3;
        }
        $alone_user_pool =app('repo_config')->queryConfig('POOL_USER');
        if ($alone_user_pool){
            $user_info =app('repo_user_data')->getUserInfo($alone_user_pool->value);
            $alone_user_pool_name = $user_info->nickname;
            $alone_user_pool_code = $user_info->user_code;
        }else{
            $alone_user_pool_name= '';
            $alone_user_pool_code= '';
        }

        $data = [
            'alone_user_pool_name'=>$alone_user_pool_name,
            'alone_user_pool_code'=>$alone_user_pool_code,
            'radio_pool_type'=>$radio_pool_type,
            'pot_name' => $this->pot_name,
            'all_number' => $all_number,
            'all_money' => $all_money,
            'list' => $giftList,
        ];
        return Response::sendData($data);
    }


    /**
     * @api {post} /fish/next_list  钓鱼下期奖池列表
     * @apiVersion 0.1.0
     * @apiName 钓鱼下期奖池列表
     * @apiGroup 游戏管理
     * @apiParam {number} type    1一连2十连3百连4独立奖池5爆率
     * @apiSuccessExample 成功响应
        {
            "code": 200,
            "data": {
            "pot_name(奖池名字)": "10连奖池",
            "all_number（剩余礼物数量）": 1000016312,
            "all_money（剩余礼物总金币数量）": 427198848614562,
            "list": [
                    {
                    "id（主键ID）": 3735,
                    "number（数量）": 9190,
                    "gift_id（奖品ID）": 156,
                    "ratio（爆率）": 0,
                    "name（奖品名字）": "音符",
                    "picture（图片）": "http://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/admin_images/5e61e230d6521.png",
                    "price（价格）": 10
                    }
                ]
            }
        }
     *
     */


    public function nextPrizeList(Request $request) {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'type' => 'filled|numeric|in:1,2,3,4,5',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $type = $request->input('type', 3);
        $giftList = app('repo_win_jackpot_next')->nextGiftList($type);
        //剩余礼物数量
        $all_number = 0;
        //剩余礼物总金币数量
        $all_money = 0;
        if ($giftList) {
            foreach ($giftList as $key => &$value) {
                $all_number += $value['number'];
                $value['name'] = $value['gift']['name'];
                $value['picture'] = $value['gift']['picture'];
                $value['price'] = $value['gift']['price'];
                $all_money += ($value['number'] * $value['gift']['price']);
                unset($value['gift']);
            }
        }
        $data = [
            'pot_name' => $this->pot_name,
            'all_number' => $all_number,
            'all_money' => $all_money,
            'list' => $giftList,
        ];
        return Response::sendData($data);
    }


    /**
     * @api {post} /fish/add_next_prize    新增钓鱼下期奖池
     * @apiVersion 0.1.0
     * @apiName 新增钓鱼下期奖池
     * @apiGroup 游戏管理
     * @apiParam {number} gift_id   礼物ID，必传
     * @apiParam {number} type   礼物类型：1一连2十连3百连4独立奖池5爆率，必传
     * @apiParam {number} number   数量，非必传
     * @apiParam {number} ratio   爆率，非必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 93050  新增钓鱼下期奖池失败
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 93050,
     *          "message": "errorMsg"
     *      }
     *
     */

    public function addNextPrize(Request $request) {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'gift_id' => 'required|numeric',
                'type' => 'required|numeric',
                'number' => 'filled|numeric',
                'ratio' => 'filled|numeric',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $param = $request->only(['gift_id', 'type']);
        $number = $request->input('number', 0);
        $ratio = $request->input('ratio', 0);
        $param['number'] = $number;
        $param['ratio'] = $ratio;
        app('repo_win_jackpot_next')->addNextPrize($param);
        return Response::sendData(true);
    }
    /**
     * @api {post} /fish/add_this_prize    新增钓鱼本期奖池
     * @apiVersion 0.1.0
     * @apiName 新增钓鱼本期奖池
     * @apiGroup 游戏管理
     * @apiParam {number} gift_id   礼物ID，必传
     * @apiParam {number} type   礼物类型：1一连2十连3百连4独立奖池5爆率，必传
     * @apiParam {number} number   数量，非必传
     * @apiParam {number} ratio   爆率，非必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 93040  新增钓鱼本期奖池失败
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 93040,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function addThisPrize(Request $request) {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'gift_id' => 'required|numeric',
                'type' => 'required|numeric',
                'number' => 'filled|numeric',
                'ratio' => 'filled|numeric',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $param = $request->only(['gift_id', 'type']);
        $number = $request->input('number', 0);
        $ratio = $request->input('ratio', 0);
        $param['number'] = $number;
        $param['ratio'] = $ratio;
        app('repo_win_jackpot')->addThisPrize($param);
        return Response::sendData(true);
    }

    /**
     * @api {post} /fish/save_this_prize 编辑钓鱼本期奖池
     * @apiVersion 0.1.0
     * @apiName 编辑钓鱼本期奖池
     * @apiGroup 游戏管理
     * @apiParam {number} id   主键ID，必传
     * @apiParam {number} gift_id   礼物ID，必传
     * @apiParam {number} type   礼物类型：1一连 2十连 3百连 4独立奖池 5爆率，必传
     * @apiParam {number} number   数量，非必传
     * @apiParam {number} ratio   爆率，非必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 93041  编辑钓鱼本期奖池失败
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 93041,
     *          "message": "errorMsg"
     *      }
     */
    public function saveThisPrize(Request $request) {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'id' => 'required|numeric',
                'gift_id' => 'required|numeric',
                'type' => 'required|numeric',
                'number' => 'filled|numeric',
                'ratio' => 'filled|numeric',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $param = $request->only(['id', 'gift_id', 'type']);
        $number = $request->input('number', '');
        $ratio = $request->input('ratio', '');
        if ($number !== '') {
            $param['number'] = $number;
        }
        if ($ratio !== '') {
            $param['ratio'] = $ratio;
        }
        app('repo_win_jackpot')->saveThisPrize($param);
        return Response::sendData(true);
    }

    /**
     * @api {post} /fish/save_next_prize 编辑钓鱼下期奖池
     * @apiVersion 0.1.0
     * @apiName 编辑钓鱼下期奖池
     * @apiGroup 游戏管理
     * @apiParam {number} id   主键ID，必传
     * @apiParam {number} gift_id   礼物ID，必传
     * @apiParam {number} type   礼物类型：1一连 2十连 3百连 4独立奖池 5爆率，必传
     * @apiParam {number} number   数量，非必传
     * @apiParam {number} ratio   爆率，非必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 93051  编辑钓鱼下期奖池失败
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 93051,
     *          "message": "errorMsg"
     *      }
     */

    public function saveNextPrize(Request $request) {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'id' => 'required|numeric',
                'gift_id' => 'required|numeric',
                'type' => 'required|numeric',
                'number' => 'filled|numeric',
                'ratio' => 'filled|numeric',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $param = $request->only(['id', 'gift_id', 'type']);
        $number = $request->input('number', '');
        $ratio = $request->input('ratio', '');
        if ($number !== '') {
            $param['number'] = $number;
        }
        if ($ratio !== '') {
            $param['ratio'] = $ratio;
        }
        app('repo_win_jackpot_next')->saveNextPrize($param);
        return Response::sendData(true);
    }


    /**
     * @api {post} /fish/del_this_prize 删除钓鱼本期奖池
     * @apiVersion 0.1.0
     * @apiName 删除钓鱼本期奖池
     * @apiGroup 游戏管理
     *
     * @apiParam {string} ids   主键（多个用逗号隔开），必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 93043  删除礼物失败
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 93043,
     *          "message": "errorMsg"
     *      }
     *
     */

    public function delThisPrize(Request $request) {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'ids' => 'required',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $ids = $request->input('ids');
        $arr_ids = explode(',', $ids);
        app('repo_win_jackpot')->delThisPrize($arr_ids);
        return Response::sendData(true);
    }


    /**
     * @api {post} /fish/del_next_prize 删除钓鱼下期奖池
     * @apiVersion 0.1.0
     * @apiName 删除钓鱼下期奖池
     * @apiGroup 游戏管理
     *
     * @apiParam {string} ids   主键（多个用逗号隔开），必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 93053  删除礼物失败
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 93053,
     *          "message": "errorMsg"
     *      }
     *
     */

    public function delNextPrize(Request $request) {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'ids' => 'required',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $ids = $request->input('ids');
        $arr_ids = explode(',', $ids);
        app('repo_win_jackpot_next')->delNextPrize($arr_ids);
        return Response::sendData(true);
    }


    /**
     * @api {post} /fish/edit_all_list  钓鱼一键修改本期礼物列表
     * @apiVersion 0.1.0
     * @apiName 钓鱼一键修改本期礼物列表
     * @apiGroup 游戏管理
     * @apiParam {number} type  类型：1一连 2十连 3百连 4独立奖池 5爆率，必传
     * @apiSuccessExample 成功响应
        {
            "code": 200,
            "data": [
                    {
                    "id（主键）": 3736,
                    "number（数量）": 10,
                    "gift_id（礼物id）": 82,
                    "ratio（爆率）": 0,
                    "name（礼物名字）": "海豚池"
                    }
                ]
        }
     *
     */
    public  function  editAllList(Request $request){
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'type' => 'required|in:1,2,3,4,5',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $giftList = app('repo_win_jackpot')->thisGiftList($this->type);
        if ($giftList) {
            foreach ($giftList as $key => &$value) {
                $value['name'] = $value['gift']['name'];
                unset($value['gift']);
            }
        }
        return Response::sendData($giftList);
    }

    /**
     * @api {post} /fish/edit_all_next_list  钓鱼一键修改下期礼物列表
     * @apiVersion 0.1.0
     * @apiName 钓鱼一键修改下期礼物列表
     * @apiGroup 游戏管理
     * @apiParam {number} type  类型：1一连 2十连 3百连 4独立奖池 5爆率，必传
     * @apiSuccessExample 成功响应
        {
            "code": 200,
            "data": [
                {
                    "id（主键）": 3736,
                    "number（数量）": 10,
                    "gift_id（礼物id）": 82,
                    "ratio（爆率）": 0,
                    "name（礼物名字）": "海豚池"
                }
            ]
        }
     *
     */
    public  function  editAllNextList(Request $request){
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'type' => 'required|in:1,2,3,4,5',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $giftList = app('repo_win_jackpot_next')->nextGiftList($this->type);
        if ($giftList) {
            foreach ($giftList as $key => &$value) {
                $value['name'] = $value['gift']['name'];
                unset($value['gift']);
            }
        }
        return Response::sendData($giftList);
    }


    /**
     * @api {post} /fish/edit_all_gifts  钓鱼一键修改本期礼物
     * @apiVersion 0.1.0
     * @apiName 钓鱼一键修改本期礼物
     * @apiGroup 游戏管理
     * @apiParam {array} list  修改的所有礼物信息，必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     *
     */
    public function  editAllGifts(Request $request){
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'list' => 'required|array',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $list = $request->input('list');
//        $list=[
//            [
//                'id'=>3737,
//                'name'=>999,
//                'gift_id'=>999,
//                'number'=>999,
//                'ratio'=>1,
//
//            ],
//            [
//                'id'=>3736,
//                'name'=>7888,
//                'gift_id'=>999,
//                'number'=>999,
//                'ratio'=>1,
//            ]
//        ];
        if ($list){
            foreach ($list as $key=>&$value){
                if ($value){
                    unset($value['name']);
                    app('repo_win_jackpot')->editAllGifts($value);
                }
            }
        }
        return Response::sendData(true);
    }

    /**
     * @api {post} /fish/edit_next_all_gifts  钓鱼一键修改下期礼物
     * @apiVersion 0.1.0
     * @apiName 钓鱼一键修改下期礼物
     * @apiGroup 游戏管理
     * @apiParam {array} list  修改的所有礼物信息，必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     *
     */

    public function  editNextAllGifts(Request $request){
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'list' => 'required|array',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $list = $request->input('list');
        if ($list){
            foreach ($list as $key=>&$value){
                if ($value){
                    unset($value['name']);
                    app('repo_win_jackpot_next')->editNextAllGifts($value);
                }
            }
        }
        return Response::sendData(true);
    }

    /**
     * @api {post} /fish/win_gift_list  中奖记录列表
     * @apiVersion 0.1.0
     * @apiName 中奖记录列表
     * @apiGroup 游戏管理
     * @apiParam {string} keyword  用户编号或者昵称，非必传
     * @apiParam {number} gift_id  礼物id，非必传
     * @apiParam {string} start_time  开始时间，非必传
     * @apiParam {string} end_time  结束时间，非必传
     * @apiParam {number} page  页数，非必传
     * @apiParam {number} perPage  每页条数，非必传
     * @apiSuccessExample 成功响应
        {
            "code": 200,
            "data": {
                "list": [
                    {
                        "id(主键id)": 137655,
                        "user_id（用户id）": 547196,
                        "gift_id（礼物id）": 156,
                        "add_time（时间）": 1584740299,
                        "number（数量）": 1,
                        "nickname（昵称）": "cl.触碰/",
                        "user_code（用户编号）": 700741,
                        "gift_name（礼物名字）": "音符"
                    }
                ],
                "meta": {
                    "page（页数）": 1,
                    "limit（每页数量）": 3,
                    "next（下一页）": "http://admin.rh/fish/win_gift_list?=2",
                    "total（总数）": 8963
                }
            }
        }
     *
     */
    public   function  winGiftList(Request $request){
        $gift_id =$request->input('gift_id','');
        $keyword =$request->input('keyword','');
        $start_time =$request->input('start_time','');
        $end_time =$request->input('end_time','');
        $page =$request->input('page',1);
        $perPage =$request->input('perPage',20);
        $where=[];
        if ($gift_id){
            $where[] =['gift_id',$gift_id];
        }
        if ($keyword){
            $user_ids =  app('repo_user_data')->getAllUserIds($keyword);
            $user_ids =array_column($user_ids,'user_id');
            $where[] = [function($query) use ($user_ids){
                $query->whereIn('user_id', $user_ids);
            }];
        }
        $time = CommonService::transTime($start_time,$end_time);
        $search_time = [$time['start_at'],$time['end_at']];
        $where[] = [function($query) use ($search_time){
            $query->whereBetween('add_time', $search_time);
        }];
        $list =  app('repo_win_log')->winGiftList($where,$perPage,$page);
        if ($list){
            foreach ($list as $k=>&$v){
                $v->nickname= $v->user_data->nickname;
                $v->user_code= $v->user_data->user_code;
                $v->gift_name= $v->gift->name;
                unset($v->user_data);
                unset($v->gift);
            }
        }
        return Response::sendList($list);
    }




    /**
     * @api {get} /fish/win_gift_export  钓鱼导出中奖记录
     * @apiVersion 0.1.0
     * @apiName 钓鱼导出中奖记录
     * @apiGroup 游戏管理
     * @apiParam {string} keyword  用户编号或者昵称，非必传
     * @apiParam {number} gift_id  礼物id，非必传
     * @apiParam {string} start_time  开始时间，非必传
     * @apiParam {string} end_time  结束时间，非必传
     * @apiParam {number} page  页数，非必传
     * @apiParam {number} perPage  每页条数，非必传
     * @apiSuccessExample 成功响应
     * 导出一个xls格式的钓鱼中奖记录数据表格
     */

    public   function  winGiftExport(Request $request){
        $gift_id =$request->input('gift_id','');
        $keyword =$request->input('keyword','');
        $start_time =$request->input('start_time','');
        $end_time =$request->input('end_time','');
        $page =$request->input('page',1);
        $perPage =$request->input('perPage',20);
        $where=[];
        if ($gift_id){
            $where[] =['gift_id',$gift_id];
        }
        if ($keyword){
            $user_ids =  app('repo_user_data')->getAllUserIds($keyword);
            $user_ids =array_column($user_ids,'user_id');
            $where[] = [function($query) use ($user_ids){
                $query->whereIn('user_id', $user_ids);
            }];
        }
        $time = CommonService::transTime($start_time,$end_time);
        $search_time = [$time['start_at'],$time['end_at']];
        $where[] = [function($query) use ($search_time){
            $query->whereBetween('add_time', $search_time);
        }];
        $list =  app('repo_win_log')->winGiftList($where,$perPage,$page);
        if ($list){
            foreach ($list as $k=>&$v){
                $v->nickname= $v->user_data->nickname;
                $v->user_code= $v->user_data->user_code;
                $v->gift_name= $v->gift->name;
                $v->add_times=  date('Y-m-d H:i:s', $v->add_time);
                unset($v->user_data);
                unset($v->gift);
                unset($v->user_id);
                unset($v->gift_id);
                unset($v->add_time);
            }
            $list = $list->toArray();
        }
        if (isset($list['data'])){
            $list =$list['data'];
            $headings = [
                '序号',
                '奖品数量',
                '用户昵称',
                '用户编号',
                '奖品名称',
                '中奖时间',
            ];
            return app('repo_export')->export(6, $request->all(), $headings, $list);
        }
    }



}
